from django.db import models
from business.models import *


class DrugList(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    date = models.DateField('Data emiterii', unique=True, help_text='Data la care intra in vigoare lista')

    def __unicode__(self):
        return self.date.strftime('DrugList: %d %b %Y')


class DrugListDrug(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    drug_list = models.ForeignKey('DrugList', verbose_name='Lista de medicamente', help_text='Lista din care face parte medicamentul descris')
    drug = models.ForeignKey('Drug', verbose_name='Medicament')

    max_price_per_unit = models.DecimalField('Pret Max/UT', max_digits=10, decimal_places=6, help_text='Pretul maximal per unitate terapeutica')

    # TODO : Adauga coloane pentru celelalte detalii publicate despre un medicament intr-o lista de medicamente CNAS
    # TODO : Check if this DrugListDrug model can be a middle table for a many2many between DrugList and Drug


class Drug(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    name = models.CharField('Denumire', max_length=50, help_text='Denumirea comerciala a medicamentului')
    comercial_code = models.CharField('Cod comercial', max_length=20, unique=True, help_text='Codul comercial unic')
    prescription_type = models.ForeignKey('DrugPrescriptionType', verbose_name='Tip prescriere', help_text='Tipul de prescriere (RX, OTC, PNS)')
    dci = models.ForeignKey('Dci', verbose_name='DCI', help_text='Denumirea comuna internationala')
    atc_code = models.CharField('Cod ATC', max_length=7, help_text='Codul de clasificare Anatomical Therapeutic Chemical (ATC)')
    pharmaceutical_form = models.ForeignKey('PharmaceuticalForm', verbose_name='Forma farmaceutica', help_text='Forma farmaceutica a medicamentului')
    concentration = models.CharField('Concentratie', max_length=20, help_text='Concentratia substantei active in medicament')

    producer = models.ForeignKey('DrugProducer', verbose_name='Producator', help_text='Producatorul medicamentului')
    marketing_authorisation = models.CharField('Numar APP', max_length=15, blank=True, null=True, help_text='Numarul autorizatiei de punere pe piata (APP)')
    # Careful with the next one because if points to the DrugProducer table with is also pointed to by
    # the producer field. Django will spit out that it requires a related_name argument to one of them
    # such that it creates two different relation sets to the same DrugProducer model:
    # marketing_authorisation_holder = models.ForeignKey('DrugProducer', verbose_name='Detinator APP', help_text='Detinatorul autorizatiei de punere pe piata (APP)')
    marketing_authorisation_issued_date = models.DateField()

    vat = models.ForeignKey(Vat, verbose_name='Cota TVA', help_text='Cota procentuala a taxei pe valoare adaugata')

    # is_special = models.BooleanField('Legi?', help_text='Prevazut de legi speciale?')  # nu am gasit nimic in nom_medicamente pe coloana asta
    is_generic_drug = models.BooleanField('Generic?', help_text='Este medicament generic?')
    is_narcotic = models.BooleanField('Narcotic?', help_text='Este un medicament narcotic?')

    barcode = models.CharField('Barcode', max_length=50, help_text='Codul de bare')

    administration = models.TextField('Administrare', blank=True, help_text='Indicatii despre administrare (optional)')
    comment = models.TextField('Comentariu', blank=True, help_text='Memento (optional)')

    class Meta:
        ordering = ['name']
        verbose_name = 'medicament'
        verbose_name_plural = 'medicamente'
        unique_together = (('comercial_code', 'name'),)


class PharmaceuticalForm(models.Model):
    """ http://www.hc-sc.gc.ca/dhp-mps/prodpharma/notices-avis/abbrev-abrev/pharmaceuticalform_formeposologique-eng.php """
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    name = models.CharField('Denumire', max_length=80, unique=True, help_text='Denumirea formei farmaceutice (ex.: Sol. Inj.)')  # poate servi si ca coloana prezentare si coloana um pentru Drug
    form = models.CharField('Cod FF', max_length=5, unique=True, help_text='Codul formei farmaceutice')
    # TODO : Intra si cant/ut aici?


class DrugProducer(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    name = models.CharField('Denumire', max_length=50, unique=True, help_text='Denumirea producatorului de medicamentului')


class Dci(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    name = models.CharField('DCI', max_length=50, unique=True, help_text='Denumirea comuna internationala (DCI) pentru o substanta activa')


class DrugPrescriptionType(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    ptype = models.CharField('Tip', max_length=10, unique=True, help_text='Tipul de prescriere')
